angular.module('myApp.login',['ngRoute'])
.config(['$routeProvider',function ($routeProvider) {
    $routeProvider.when('/login',{
        templateUrl:'login/login.html',
        controller:'loginCtrl'
    });
}])

.controller('loginCtrl',['$scope','$rootScope','$http','$location','AuthService',function (
    $scope,$rootScope,$http,$location,authService
) {
    $scope.error=false;
    $rootScope.selectedTab=$location.path() || '/';
    $scope.credentials={};
    $scope.login=function(){
        $http({
            url:'auth/login',
            method:'POST',
            data:$scope.credentials,
            headers:authService.createAuthorizationTokenHeader()
        })
            .success(function (res) {
              //  console.log('执行');
                $rootScope.autenticated=true;
                authService.setJwtToken(res.access_token);
                $location.path('#/');
                $rootScope.selectedTab="/";
                $scope.error=false;
            })
            .catch(function(){
                authService.removeJwtToken();
                $rootScope.autenticated=false;
                $scope.error=true;
            });
    };

}]);